﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Rugicon
{ 
    /// <summary>
    /// Wraps Live Messenger Desktop Client data.
    /// </summary>
    public static class LiveMessenger
    {
        /// <summary>
        /// Gets all custom emoticons in all Live accounts in the current PC.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<LiveEmoticon> GetEmoticons()
        {                 
           List<LiveEmoticon> emoticons = new List<LiveEmoticon>();
           
           // If the Live Messenger directory doesn't exists (i.e. is not installed),
           // don't fail but return none.
           if (!Directory.Exists(RootFolderPath))
               return emoticons;

           // Each Live Messenger account has a separate folder
           string[] accountFolders = Directory.GetDirectories(RootFolderPath, "*@*", SearchOption.TopDirectoryOnly);

           foreach (string accountFolder in accountFolders)
           {
               string emoticonsFolder = Path.Combine(accountFolder, @"ObjectStore\CustomEmoticons");

               // The folder may not exists if no custom emoticons were added
               if (!Directory.Exists(emoticonsFolder))
                   continue;

               // Each .dt2 file is, actually, a png or gif image
               string[] rawEmoticonFiles = Directory.GetFiles(emoticonsFolder, "*.dt2", SearchOption.TopDirectoryOnly);

               foreach (string rawEmoticonFile in rawEmoticonFiles)
               {
                   // Calculate the hash (md5) of the file to compare if an equal 
                   // emoticon was already added. This happens when the user have
                   // multiple Live Messenger accounts.

                   using (var md5 = new MD5CryptoServiceProvider())
                   {
                       var hash = md5.ComputeFileHash(rawEmoticonFile);
                       var query = (from e in emoticons
                                    where e.Hash.SequenceEqual(hash)
                                    select e).SingleOrDefault();

                       // If the hash isn't found, add the emoticon
                       if (query == null)
                           emoticons.Add(new LiveEmoticon { Filename = rawEmoticonFile, Hash = hash });
                   }   
               }
           }

           return emoticons;
        }

        /// <summary>
        /// Root folder of Live Messenger data.
        /// </summary>
        public static string RootFolderPath
        {
            get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\Messenger\"); }
        }
    }

    /// <summary>
    /// Models a Live Messenger custom emoticon.
    /// </summary>
    public class LiveEmoticon
    {
        /// <summary>
        /// The hash of the emoticon image.
        /// </summary>
        public byte[] Hash { get; set; }

        /// <summary>
        /// The absolute path to the emoticon file.
        /// </summary>
        public string Filename { get; set; }
    }
}
